package com.party.web.web.controller.wechat;

import com.party.core.model.member.DeployType;
import com.party.core.model.member.MemberExternalApp;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.member.IMemberExternalAppService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.web.utils.RealmUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 公众号管理
 * Created by admin on 2017/8/29.
 */
@Controller
@RequestMapping("/wechat/officeaccounts")
public class OfficeAccountsController {

    protected static Logger logger = LoggerFactory.getLogger(OfficeAccountsController.class);

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private IMemberExternalAppService memberExternalAppService;


    //根据用户的状态判断 如果未绑定则跳转至绑定提示页，已绑定则跳转至主页
    @RequestMapping("view")
    public ModelAndView index(){
        MemberGroup member = RealmUtils.getCurrentUser();
        //授权判定
        boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
        if(!isAuthorize){
            //没有授权,跳转至绑定提示页
            return new ModelAndView("wechat/boundTips");
        }

        //已授权，获取授权者信息，跳转至绑定设置页
        ModelAndView mv = new ModelAndView("wechat/boundSetting");
        MemberExternalApp memberExternalApp
                = memberExternalAppService.findByMemberIdAndDeployType(member.getId(), DeployType.AUTHORIZATION.getCode());
        WechatOpenMember wechatOpenMember = wechatOpenMemberService.get(memberExternalApp.getExternalAccountId());
        mv.addObject("grantor",wechatOpenMember);

        return mv;
    }

    //绑定
}
